package com.macbulldev.poc.testgin.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.macbulldev.poc.testgin.domain.LiabilityAmount;
import com.macbulldev.poc.testgin.domain.PolicyForm;
import com.macbulldev.poc.testgin.domain.TransactionQuestion;
import com.macbulldev.poc.testgin.domain.TrialCase;
import com.macbulldev.poc.testgin.domain.TrialGroup;


@org.springframework.roo.addon.web.mvc.controller.RooWebScaffold(path = "liabilityamount", automaticallyMaintainView = true, formBackingObject = com.macbulldev.poc.testgin.domain.LiabilityAmount.class)
@org.springframework.web.bind.annotation.RequestMapping("/liabilityamount/**")
@org.springframework.stereotype.Controller
public class LiabilityAmountController {
	
	
	private static final Logger LOGGER = Logger.getLogger(LiabilityAmount.class);
	
	@RequestMapping(value = "/liabilityamount/ajax/add", method = RequestMethod.POST)    
    public String processGroupAdd(@RequestParam(value="data", required=false) String data,ModelMap modelMap){
		
		String[] strArr = data.split(":");
		
		TrialGroup tg = new TrialGroup();
		
		LOGGER.error("inside add method");
		
		for(String myStr : strArr)
		{
			
			LOGGER.error("inside First STring loop");
			
			if(myStr.trim().length() > 0)
			{
				
				LOGGER.error("inside if string is greater than 0:  strLen:  "+myStr.split("&").length);
				
				String importName = myStr.split("&")[0].trim();
				String amount = myStr.split("&")[1].trim();
				String premium = myStr.split("&")[2].trim();
				String groupId = myStr.split("&")[3].trim();
				String policyFormId = myStr.split("&")[4].trim();
				String pqs = "";
				
				if(myStr.split("&").length > 5)
				{
					pqs = myStr.split("&")[5].trim();
				}
				
				LOGGER.error(importName+" :  "+amount+"  :  "+premium+"  :  "+groupId+"  :  "+policyFormId+"  :  "+pqs);

				List<TrialCase> tcList = TrialGroup.findTrialGroup(new Long(groupId)).getTrialCases();

				tg.setId(new Long(groupId));
				
				TrialCase tc = tcList.get(0);
				
				LiabilityAmount la = new LiabilityAmount();
				
				la.setImportName(importName);
				la.setAmount(new BigDecimal(amount));
				la.setPremium(new BigDecimal(premium));
				la.setTrialGroup(TrialGroup.findTrialGroup(new Long(groupId)));
				la.setTrialCase(tc);
				la.setPolicyForm(PolicyForm.findPolicyForm(new Long(policyFormId)));
				
				la.persist();				
				
				//build policy questions
				
				if(pqs.length() > 0)
				{
				
					String[] pqArr = pqs.split("#");
				
					for(String myPQ : pqArr)
					{
					
						LOGGER.error(myPQ);
					
						String question = myPQ.split("@")[0];
						String answer	= myPQ.split("@")[1];

						TransactionQuestion tq = new TransactionQuestion();
						tq.setQuestion(question);
						tq.setAnswer(answer);
						tq.setType("policy");
						tq.setLiabilityAmount(la);
						tq.persist();
					}//end foreach policy question
				
				}
				
			}//end if string is greater than zero
		}//end for each liablity amount string
		
		modelMap.addAttribute("msg","success");
		
		return "scenario/ajax_success";   
	}
	
	@RequestMapping(value = "/liabilityamount", method = RequestMethod.POST)    
    public String create(@Valid LiabilityAmount liabilityAmount, BindingResult result, ModelMap modelMap) {    
        if (liabilityAmount == null) throw new IllegalArgumentException("A liabilityAmount is required");        
        if (result.hasErrors()) {        
            modelMap.addAttribute("liabilityAmount", liabilityAmount);            
            return "liabilityamount/create";            
        }        
        liabilityAmount.persist();        
        return "redirect:/liabilityamount/" + liabilityAmount.getId();        
    }

	@RequestMapping(value = "/liabilityamount/form", method = RequestMethod.GET)    
    public String createForm(ModelMap modelMap) {    
        modelMap.addAttribute("liabilityAmount", new LiabilityAmount());        
        return "liabilityamount/create";        
    }

	@RequestMapping(value = "/liabilityamount/{id}", method = RequestMethod.GET)    
    public String show(@PathVariable("id") Long id, ModelMap modelMap) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        modelMap.addAttribute("liabilityAmount", LiabilityAmount.findLiabilityAmount(id));        
        return "liabilityamount/show";        
    }

	@RequestMapping(value = "/liabilityamount", method = RequestMethod.GET)    
    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, ModelMap modelMap) {    
        if (page != null || size != null) {        
            int sizeNo = size == null ? 10 : size.intValue();            
            modelMap.addAttribute("liabilityamounts", LiabilityAmount.findLiabilityAmountEntries(page == null ? 0 : (page.intValue() - 1) * sizeNo, sizeNo));            
            float nrOfPages = (float) LiabilityAmount.countLiabilityAmounts() / sizeNo;            
            modelMap.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));            
        } else {        
            modelMap.addAttribute("liabilityamounts", LiabilityAmount.findAllLiabilityAmounts());            
        }        
        return "liabilityamount/list";        
    }

	@RequestMapping(method = RequestMethod.PUT)    
    public String update(@Valid LiabilityAmount liabilityAmount, BindingResult result, ModelMap modelMap) {    
        if (liabilityAmount == null) throw new IllegalArgumentException("A liabilityAmount is required");        
        if (result.hasErrors()) {        
            modelMap.addAttribute("liabilityAmount", liabilityAmount);            
            return "liabilityamount/update";            
        }        
        liabilityAmount.merge();        
        return "redirect:/liabilityamount/" + liabilityAmount.getId();        
    }

	@RequestMapping(value = "/liabilityamount/{id}/form", method = RequestMethod.GET)    
    public String updateForm(@PathVariable("id") Long id, ModelMap modelMap) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        modelMap.addAttribute("liabilityAmount", LiabilityAmount.findLiabilityAmount(id));        
        return "liabilityamount/update";        
    }

	@RequestMapping(value = "/liabilityamount/{id}", method = RequestMethod.DELETE)    
    public String delete(@PathVariable("id") Long id, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        LiabilityAmount.findLiabilityAmount(id).remove();        
        return "redirect:/liabilityamount?page=" + ((page == null) ? "1" : page.toString()) + "&size=" + ((size == null) ? "10" : size.toString());        
    }
}
